package com.bsb.biz.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bsb.base.common.util.StringUtils;
import com.bsb.base.model.BsbExpertAsk;
import com.bsb.base.model.BsbExpertAskExample;
import com.bsb.base.model.BsbExpertAskExample.Criteria;
import com.bsb.biz.service.BsbExpertAskService;

@Controller
@RequestMapping("/bsbExpertAsk")
public class BsbExpertAskController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbExpertAsk/list.do";
	
	@Resource
	private BsbExpertAskService bsbExpertAskService;
	
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(
			Model model,String quId,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
//		int maxPageNum = 0;
//		if (pageSize == null) {
//			pageSize = defaultPageSize;
//		}
		BsbExpertAskExample bsbExpertAskExample = new BsbExpertAskExample();
		Criteria criteria = bsbExpertAskExample.createCriteria();
		if(StringUtils.isNotEmpty(quId)){
			criteria.andQuIdEqualTo(quId);
		}
//		int totalCount = bsbExpertAskService.countBsbExpertAsk(bsbExpertAskExample);
//		if(totalCount%pageSize == 0){
//			maxPageNum = totalCount/pageSize;
//		}else{
//			maxPageNum = totalCount/pageSize+1;
//		}
//		if (pageNum == null || pageNum < 1) {
//			pageNum = 1;
//		}else {
//			if(maxPageNum > 0 && pageNum > maxPageNum) {
//				pageNum = maxPageNum;
//			}
//		}
//		bsbExpertAskExample.setPageIndex((pageNum - 1) * pageSize);
//		bsbExpertAskExample.setPageSize(pageSize);
		bsbExpertAskExample.setOrderByClause("createDate DESC");
		List<BsbExpertAsk> bsbExpertAsks = bsbExpertAskService.selectByExample(bsbExpertAskExample);
//		model.addAttribute("totalCount", totalCount);
//		model.addAttribute("pageNum", pageNum);
//		model.addAttribute("pageSize", pageSize);
//		if(totalCount%pageSize == 0){
//			model.addAttribute("maxPageNum", totalCount/pageSize);
//		}else{
//			model.addAttribute("maxPageNum", totalCount/pageSize+1);
//		}
		model.addAttribute("bsbExpertAsks", bsbExpertAsks);
		return "/BsbExpertAsk/list";
	}
	
	@RequiresPermissions("bsbExpertAsk:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toAdd(Model model) {
		return "/BsbExpertAsk/add";
	}

	@RequiresPermissions("bsbExpertAsk:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String add(Model model, @ModelAttribute("bsbExpertAsk") BsbExpertAsk bsbExpertAsk) {
		bsbExpertAskService.addBsbExpertAsk(bsbExpertAsk);
		return redirect;
	}
	
	@RequiresPermissions("bsbExpertAsk:delete")
	@RequestMapping(value = "/{uuid}/delete", method = RequestMethod.GET)
	public String del(Model model,
			@PathVariable("uuid") String uuid) {
		bsbExpertAskService.delBsbExpertAskByUuid(uuid);
		return redirect;
	}
	
	@RequiresPermissions("bsbExpertAsk:update")
	@RequestMapping(value = "/{uuid}/update", method = RequestMethod.GET)
	public String toEdit(Model model,
			@PathVariable("uuid") String uuid) {
		BsbExpertAsk bsbExpertAsk = bsbExpertAskService.getBsbExpertAskByUuid(uuid);
		model.addAttribute("bsbExpertAsk", bsbExpertAsk);
		return "/BsbExpertAsk/edit";
	}

	@RequiresPermissions("bsbExpertAsk:update")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String edit(Model model, @ModelAttribute("bsbExpertAsk") BsbExpertAsk bsbExpertAsk) {
		bsbExpertAskService.updateBsbExpertAsk(bsbExpertAsk);
		return redirect;
	}
	
	@RequiresPermissions("bsbExpertAsk:view")
	@RequestMapping(value = "/{uuid}/view", method = RequestMethod.GET)
	public String view(Model model,
			@PathVariable("uuid") String uuid) {
		BsbExpertAsk bsbExpertAsk = bsbExpertAskService.getBsbExpertAskByUuid(uuid);
		model.addAttribute("bsbExpertAsk", bsbExpertAsk);
		return "/BsbExpertAsk/view";
	}
}
